Add foreign key constraint

ALTER TABLE target_table ADD CONSTRAINT fk_t_tablename FOREIGN KEY (column_name) REFERENCES parent_table (column_name);

Description

Adds a foreign key constraint to a table.

Arguments

String target_table

The table you want to add a foreign key on.

String fk_t_tablename

The name of the constraint in the format fk_(first character of current table)_(parent table)

String column_name

The name of the foreign key column.

Examples

Example 1

This command will create a foreign key constraint on the userid column

ALTER TABLE admins ADD CONSTRAINT fk_a_users FOREIGN KEY (userid) REFERENCES users (id);